/**
 * mvc pattern - mediator
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2009. 11. 19 
*/
package com.cmiscm.mvc
{
	import __AS3__.vec.Vector;
	
    /**
     * MVC Mediator - Singleton Pattern.
     * 
     * @example
     * <listing>
     * // Add Mediator
     * View.getInstance.addMediator(MenuMediator.NAME, new MenuMediator());
     * 
     * // Get Mediator
     * var menu:MenuMediator = View.getInstance.getMediator(MenuMediator.NAME) as MenuMediator;
     * 
     * // Remove Mediator
     * View.getInstance.removeMediator(MenuMediator.NAME);
     * </listing>
     * 
     * @see com.cmiscm.mvc.IView IView
     */	
    public class View extends Object
    {       
        private var _mediatorVector:Vector.<IView> = new Vector.<IView>();
        private var _nameVector:Vector.<String> = new Vector.<String>();
        
        private const NO_FOUND_MSG:String = "Mediator name is no found";
        private const ALREADY_MSG:String = "Mediator Name is already in use";
        private const SINGLETON_MSG:String = "Singleton already constructed";        
        private static var _instance:View;

        public function View()
        {         
			super(); 
            if (_instance != null) throw Error(SINGLETON_MSG);
            _instance = this;
        }        
        public static function get getInstance():View
        {
            if (_instance == null) _instance = new View();
            return _instance;
        }
		
        /**
         * Get Mediator 
         * <p>
         * return null in case not find Mediator
         * </p>
         * @param $name Mediator Name
         * @return 
         * 
         */        	
        public function getMediator($name:String):IView
        {
            var index:int = _nameVector.indexOf($name);
            if (index == -1) {
                return null;
            } else {
            	return _mediatorVector[index];
            }
        }
        
		/**
		 * Add Mediator  
		 * @param $name Mediator Name
		 * @param $mediator Mediator implements <code>IView</code>
		 * 
		 */
		public function addMediator($name:String, $mediator:IView):void
        {
            var index:int = _nameVector.indexOf($name);
            if (index == -1) {
                _mediatorVector.push($mediator);
                _nameVector.push($name);
            } else {
                throw Error(ALREADY_MSG);
            }
        }
        
        /**
         * Remove Mediator  
         * @param $name Mediator Name
         * 
         */              
        public function removeMediator($name:String):void
        {
            var index:int = _nameVector.indexOf($name);
            if (index == -1) {
                throw Error(NO_FOUND_MSG);
            } else {
            	_mediatorVector[index] = null;
	            _nameVector[index] = null;
	            _mediatorVector.splice(index, 1);
	            _nameVector.splice(index, 1);
            }
        }       

    }
}
